﻿using B.S.AuthCenter.Common;
using B.S.AuthCenter.InfrasIntructure;
using B.S.AuthCenter.InfrasIntructure.Implement;
using B.S.AuthCenter.InfrasIntructure.Interface;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Reflection;
using System.Text;

namespace B.S.AuthCenter.Api.Read.Extensions
{
    public static class ReadServiceCollectionExtensions
    {
        public static IServiceCollection AddService(this IServiceCollection services)
        {
            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
            services.AddScoped<AuthCenterDbContext>();
           // services.AddDbContext<AuthCenterDbContext>(a=>a.UseSqlServer(config));
            services.AddAutoMapper(typeof(MyMapper));
            services.AddMediatR(a => a.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()));
            services.AddScoped<JwtTools>();
            return services;
        }
        public static IServiceCollection AddDbContextService<TDbContext>(this IServiceCollection service,string? connectionString,bool enableSqlLog) where TDbContext : DbContext
        {
            return service.AddDbContext<TDbContext>(builder => 
            {
                builder.UseSqlServer(connectionString, action =>
                {
                    action.EnableRetryOnFailure(
                        maxRetryCount:10,
                        maxRetryDelay:TimeSpan.FromSeconds(60),
                        errorNumbersToAdd:null
                    );
                }).EnableSensitiveDataLogging();
                if (enableSqlLog)
                {
                    builder.UseLoggerFactory(LoggerFactory.Create(configure =>
                    {
                        configure.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
                    }));
                }
            });
        }

        public static void AddJWT(this IServiceCollection collection,WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(option =>
            {
                var secretByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                option.TokenValidationParameters = new TokenValidationParameters()
                {
                    //验证发布者
                    ValidateIssuer = true,
                    ValidIssuer = builder.Configuration["Authentication:Issuer"],
                    //验证接收者
                    ValidateAudience = true,
                    ValidAudience = builder.Configuration["Authentication:Audience"],
                    //验证是否过期
                    ValidateLifetime = true,
                    //验证私钥
                    IssuerSigningKey=new SymmetricSecurityKey(secretByte)
                };
            });
        }
    }
}
